﻿using System;
using System.Collections.Generic;
using System.Xml;
using System.Xml.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using XnaXaml.Xaml;

namespace XnaXaml.Controls
{
    public class Control
    {
        #region Xaml Properties

        public XNode XmlNode { get; set; }
        public List<XAttribute> Attributes = new List<XAttribute>();

        public object Content { get; set; }

        #endregion

        #region Xna Properties

        public Vector2 Position { get; set; }
        public int Width { get; set; }
        public int Height { get; set; }

        #endregion

        #region Control Properties

        public Action OnDraw { get; set; }

        #endregion

        #region Constructor

        public Control() { }
        public Control(XNode xmlNode)
        {
            XmlNode = xmlNode;
            ReadAttributes();
        }

        #endregion

        #region Methods

        public virtual void Generate(XamlRenderer renderer) { }
        public virtual void Render(XamlRenderer renderer) { }
        public virtual void Draw(SpriteBatch spriteBatch) { }

        public virtual void ReadAttributes()
        {
            var loadedAttributes = new List<XAttribute>((XmlNode as XElement).Attributes());
            Attributes = new List<XAttribute>();

            for (var i = 0; i < loadedAttributes.Count; i++)
            {
                var attrib = loadedAttributes[i];
                Attributes.Add(attrib);

                switch (attrib.Name.LocalName)
                {
                    case "Width":
                        Width = Convert.ToInt32(attrib.Value);
                        break;
                    case "Height":
                        Height = Convert.ToInt32(attrib.Value);
                        break;
                }
            }
        }

        

        #endregion
    }
}
